﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RootfusHostView;
using RootfusCoreComponents;

namespace RoofusUIControls
{


    /// <summary>
    /// Interaction logic for HookUpPinsDialog.xaml
    /// </summary>
    public partial class HookUpPinsDialog : UserControl
    {
        public static readonly DependencyProperty InputPinsProperty = DependencyProperty.Register(
            "InputPins",
            typeof(IList<PinConnection>),
            typeof(HookUpPinsDialog));

        /// <summary>
        /// Get or set the input pin property
        /// </summary>
        public IList<PinConnection> InputPins
        {
            get { return (IList<PinConnection>)GetValue(InputPinsProperty); }
            set { SetValue(InputPinsProperty, value); }
        }

        /// <summary>
        /// Init the hook up pins dialog box
        /// </summary>
        public HookUpPinsDialog(List<RootfusSurfaceItemWrapper> surface_items, IList<PinConnection> input_pins)
        {
            ///
            /// Get all of our various fields populated.
            /// 

            InputSurfaceItems = surface_items;
            InputPins = input_pins;
            this.Loaded += new RoutedEventHandler(HookUpPinsDialog_Loaded);

            ///
            /// Bring the connectinos and everything else up!
            /// 

            InitializeComponent();
        }

        void HookUpPinsDialog_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// We are done. Make ourselves go away.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Canvas holder = Parent as Canvas;
            holder.Children.Remove(this);
        }

        /// <summary>
        /// Called when the user wants to add a new pin to one of the infinite pin guys.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddNewPin_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b == null)
            {
                return;
            }
            PinConnectionInfinite ipin = b.CommandParameter as PinConnectionInfinite;
            if (ipin != null)
            {
                ipin.PinSources.Add(new IncomingPinConnectionInfo());
            }
        }

        /// <summary>
        /// The user wants to remove a particular hook up pin from the infinite pin collection!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b == null)
            {
                return;
            }
            PinConnectionInfinite pi = b.Tag as PinConnectionInfinite;
            IncomingPinConnectionInfo ip = b.CommandParameter as IncomingPinConnectionInfo;
            if (pi == null || ip == null)
            {
                return;
            }
            pi.PinSources.Remove(ip);
        }

        public static readonly DependencyProperty InputSurfaceItemsProperty = DependencyProperty.Register(
            "InputSurfaceItems",
            typeof(List<RootfusSurfaceItemWrapper>),
            typeof(HookUpPinsDialog));

        public List<RootfusSurfaceItemWrapper> InputSurfaceItems
        {
            set { SetValue(InputSurfaceItemsProperty, value); }
            get { return (List<RootfusSurfaceItemWrapper>)GetValue(InputSurfaceItemsProperty); }
        }

    }

    /// <summary>
    /// Class to help select a data template depending upon the value of the
    /// PinCollection.ConnectionType guy.
    /// </summary>
    internal class PinConnectionTemplateSelector : DataTemplateSelector
    {
        /// <summary>
        /// Select depending on the item type -- multi or not.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item != null && item is PinConnection && container is FrameworkElement)
            {
                PinConnection c = item as PinConnection;
                FrameworkElement wrk = container as FrameworkElement;

                switch (c.ConnectionType)
                {
                    case PinConnectionType.PinInfinite:
                        PinConnectionInfinite ipin = c as PinConnectionInfinite;
                        if (ipin.PinSources.Count == 0)
                        {
                            /// The user will want to hook up at least one, lets put it in there...
                            ipin.PinSources.Add(new IncomingPinConnectionInfo());
                        }
                        return wrk.FindResource("MultiplePinConnectionTemplate") as DataTemplate;
                    case PinConnectionType.PinSingle:
                        return wrk.FindResource("SinglePinConnectionTemplate") as DataTemplate;
                    default:
                        throw new Exception("Not implemented!");
                }
            }
            return null;
        }
    }
}
